package com.controller;

import com.util.DataSourceUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;

@WebServlet("/filter/addjob")
public class AddJobServlet extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher("/WEB-INF/jsp/xianshi.jsp").forward(req,resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
    {
        String  title=req.getParameter("addtitle");
        String content=req.getParameter("addcontent");
        String dateImageurl=req.getParameter("adddateImageurl");
        String dateTimeStr = req.getParameter("adddateTime");
        LocalDateTime dateTime= null;
        if (dateTimeStr!= null&& !dateTimeStr.trim().isEmpty()) {
            dateTime=LocalDateTime.parse(dateTimeStr);
        }
        //LocalDateTime dateTime = LocalDateTime.parse(req.getParameter("adddateTime"));
        String sql="INSERT INTO Job2 (Title,PublishDate,Content,ImageURL)  VALUES (?,?,?,?);";
        try (Connection con= DataSourceUtils.getConnection(); PreparedStatement ps = con.prepareStatement(sql) ){
            ps.setString(1, title);
            ps.setObject(2,dateTime);
            ps.setString(3, content);
            ps.setString(4, dateImageurl);
            ps.executeUpdate();
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.sendRedirect(req.getContextPath()+"/filter/houtai");
    }

}
